//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "game.h"
#include <windows.h>
#include<conio.h>

#include<vector>
#include<string.h>
#include<stdlib.h>
#include"boggle.h"
#include"boggle.cpp"
#include"UnJumble.h"
#include"UnJumble.cpp"
#include"stack.h"
#include"stack.cpp"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

HANDLE        hEvent;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//====================================
void __fastcall TForm1::Button1Click(TObject *Sender)
{

boggle myboggle;         //declare a new boggle game.

char *s1 = new char[4];
char *s2 = new char[4];
char *s3 = new char[4];
char *s4 = new char[4];
txtInput1->GetTextBuf(s1, 5);
txtInput2->GetTextBuf(s2, 5);
txtInput3->GetTextBuf(s3, 5);
txtInput4->GetTextBuf(s4, 5);

if (myboggle.SizeWord(s1) < 4 ||myboggle.SizeWord(s2) < 4 ||myboggle.SizeWord(s3) < 4 || myboggle.SizeWord(s4) < 4)
{
        Memo1->Lines->Add( "--Error: 4 letters needed in each box.--");

char *j = new char[4];
strcpy(j, "----");
txtInput1->GetTextBuf(j, 5);
Edit1->Text = j[0];
Edit2->Text = j[1];
Edit3->Text = j[2];
Edit4->Text = j[3];
char *k = new char[4];
strcpy(k, "----");
txtInput2->GetTextBuf(k, 5);
Edit5->Text = k[0];
Edit6->Text = k[1];
Edit7->Text = k[2];
Edit8->Text = k[3];
char *l = new char[4];
strcpy(l, "----");
txtInput3->GetTextBuf(l, 5);
Edit9->Text = l[0];
Edit10->Text = l[1];
Edit11->Text = l[2];
Edit12->Text = l[3];
char *m = new char[4];
strcpy(m, "----");
txtInput4->GetTextBuf(m, 5);
Edit13->Text = m[0];
Edit14->Text = m[1];
Edit15->Text = m[2];
Edit16->Text = m[3];
return;
}


char *j = new char[4];
txtInput1->GetTextBuf(j, 5);
Edit1->Text = j[0];
Edit2->Text = j[1];
Edit3->Text = j[2];
Edit4->Text = j[3];
txtInput2->GetTextBuf(j, 5);
Edit5->Text = j[0];
Edit6->Text = j[1];
Edit7->Text = j[2];
Edit8->Text = j[3];
txtInput3->GetTextBuf(j, 5);
Edit9->Text = j[0];
Edit10->Text = j[1];
Edit11->Text = j[2];
Edit12->Text = j[3];
txtInput4->GetTextBuf(j, 5);
Edit13->Text = j[0];
Edit14->Text = j[1];
Edit15->Text = j[2];
Edit16->Text = j[3];

myboggle.loadvertexs(s1,s2,s3,s4);



//myboggle.confirminput();  //the computer shows the user what the boggle board
                          //should look like.

int status = myboggle.findwords();     //look for words in the boggle board. recursive.
if (status == 1)
{
        Memo1->Text = "--Error, could not Locate Dictionary file named wordsfile.txt - This File needs to be in the same directory as this Exe--";
        return;
}

char **answers = new char*[1000];
for(int a = 0; a < 1000; a++)
        answers[a] = new char[16];

myboggle.sortanswerwords();
myboggle.getanswers(answers);

int numanswers = myboggle.getnumanswers();
Memo1->Text = "";
char *last = new char[30];

for(int x = 0; x < numanswers-1; x++)
{
        if (strcmp(last, answers[x]) != 0)
        {
                Memo1->Lines->Add(answers[x]);
                strcpy(last, answers[x]);
        }
}


}
//===================================
DWORD WINAPI ThrdFunc ( LPVOID n )
{
int        TNumber    =    (int) n;
    WaitForSingleObject ( hEvent , INFINITE );
    cout<<"Event gets signaled...."<<endl;
    return 0;

}
//=========================
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//shows all of the words that will match.

HANDLE  hThrd;
DWORD      Id;

hEvent = CreateEvent ( NULL , false , false , "my event" );

hThrd=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThrdFunc,(LPVOID)0,0,&Id);
if ( !hThrd )
{
//cout<<"Error Creating Thread ..."<<endl;
return;
}
    //    Wait For some time before giving out any signal

unjumbleclass uncryptword;

char *s = new char[30];

Edit17->GetTextBuf(s, 30);

char **answers = new char*[100000];
for(int a = 0; a < 100000; a++)
        answers[a] = new char[16];
Memo1->Text = "";
Memo1->Lines->Add("Thinking....");

if (uncryptword.SizeWord2(s) >= 13)
{

        Memo1->Visible = false;
        Label4->Visible = true;
        Label4->Caption = "This process will take more than 30 seconds because you specified more than 13 letters, the viewbox was made invisible to greatly speed up the process, it will reappear when done. Remember that for each added Letter the combonations increases Exponentially";
        Label4->Refresh();
}

SetEvent ( hEvent );

int err;
uncryptword.UnJumbleWord(s, err);
if (err == 1)
{
        Memo1->Text = "";
        Memo1->Text = "--Error, could not Locate Dictionary file named wordsfile.txt - This File needs to be in the same directory as this Exe--";

       return;
}
uncryptword.sortanswerwords();
uncryptword.getanswers(answers);
char *last = new char[30];
Memo1->Text = "";
int counter = 0;
if (CheckBox1->Checked == true)
{
        int numanswers = uncryptword.getnumanswers();
        Memo1->Text = "";
        for(int x = numanswers-1; x > 0; x--)
        {
                if (uncryptword.SizeWord2(answers[x]) == uncryptword.SizeWord2(s))
                {
                        if (strcmp(last, answers[x]) != 0)
                        {
                        Memo1->Lines->Add(answers[x]);
                        strcpy(last, answers[x]);
                        counter++;
                        }
                }
                else
                {
                        break;
                }

        }
        if (counter == 0)
                Memo1->Lines->Add("No words can be made out of all the letters: " + Edit17->Text);
//                Memo1->Lines->Add(Edit17->Text);
}
else
{
        int numanswers = uncryptword.getnumanswers();
        Memo1->Text = "";
        for(int x = 0; x < numanswers-1; x++)
        {
        if (strcmp(last, answers[x]) != 0)
        {
                Memo1->Lines->Add(answers[x]);
                strcpy(last, answers[x]);
        }
        }

    Memo1->Lines->Add("--Done--");
}
   Label4->Visible = false;
   Memo1->Visible = true;

    WaitForSingleObject ( hThrd , INFINITE );
    //    Close the handler
    CloseHandle ( hEvent );
    CloseHandle ( hThrd );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormResize(TObject *Sender)
{


if (Form1->Width > 542)
{
Panel1->Height = 0.59 * Form1->Height;
Panel1->Width = 0.68 * 542;
Panel2->Height = 0.27 * Form1->Height;
Panel2->Width = 0.68 * 542;
Panel2->Top = 0.604 * Form1->Height;
Memo1->Width = Form1->Width - Panel1->Width-30;

Memo1->Left = Form1->Width - (Form1->Width - Panel1->Width)+20;
}
else
{
Panel1->Height = 0.59 * Form1->Height;
Panel1->Width = 0.68 * Form1->Width;
Panel2->Height = (0.29 * Form1->Height);
Panel2->Width = 0.68 * Form1->Width;
Panel2->Top = (0.58 * Form1->Height);
Memo1->Width = 0.27 * Form1->Width;
Memo1->Left = (0.708 * Form1->Width);

}
}

//---------------------------------------------------------------------------

void __fastcall TForm1::Creator1Click(TObject *Sender)
{
Memo1->Text = "Including this program, All complex things need a creator. This program was Created by Eric Leschinski http://maeon3.tripod.com/main.html rosshill@tdstelme.net This program is Free But send me a message to let me know where it ends up.";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::BoggleSolver1Click(TObject *Sender)
{
Memo1->Text = "The upper portion is a boggle solver, The Game involves a Square disk of 16 Dice with letters, when you shake them up and randomize you have one minute to find as many words by starting at whatever Die you choose and continuing horizontally diagionally or vertically to make words. You can't use the same letter twice.  The reason I made this was because My friend Caitlin was much better than me at this game and I got peeved so now who's better!  hah!";

}
//---------------------------------------------------------------------------

void __fastcall TForm1::WordUnJumbler1Click(TObject *Sender)
{
Memo1->Text = "The word Un-Jumbler at the bottom takes a word with the letters mixed around and Looks through the dictionary to find matches.  If you have a Super computer you might be able to pull off more than 18 letter words but less than 16 will keep your computer busy for a minute.  The checkbox limits the search to words that use each letter once.  ";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Exit1Click(TObject *Sender)
{
return;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CCode1Click(TObject *Sender)
{
//So you can be able to see the code from within the appliation
Form1->WindowState = wsMaximized;
Memo1->Text = "";
Memo1->Lines->Add("#pragma hdrstop");
Memo1->Lines->Add("#include\"stack.h\"");
Memo1->Lines->Add("#include\"boggle.h\"");
Memo1->Lines->Add("#include<iostream.h>");
Memo1->Lines->Add("#include<fstream.h>");
Memo1->Lines->Add("#include<string.h>");
Memo1->Lines->Add("#include<iomanip>");
Memo1->Lines->Add("#include<stdlib.h>");
Memo1->Lines->Add("#include<ctype.h>");
Memo1->Lines->Add("using namespace std;");
Memo1->Lines->Add("boggle::boggle(){");
Memo1->Lines->Add("     path = new stack<char>(1000); //Need a stack of characters to keep track of what word we are looking at when to backtrack and when to move foreward");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//============================================");
Memo1->Lines->Add("void boggle::loadvertexs(char * first, char * second, char * third, char * fourth)");
Memo1->Lines->Add("{");
Memo1->Lines->Add("M = new vertex[17];           //a pointer to 16 structures of vertexes.");
Memo1->Lines->Add("answercounter = 0;");
Memo1->Lines->Add("int e = 0;");
Memo1->Lines->Add("for(int c = 0; c < 4; c++){");
Memo1->Lines->Add("     for(int d = 0; d < 4; d++){");
Memo1->Lines->Add("          M[e].x = c;");
Memo1->Lines->Add("          M[e].y = d;");
Memo1->Lines->Add("          e++;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("}");
Memo1->Lines->Add("for (int b = 0; b < 4; b++){            //this puts what the user typed into");
Memo1->Lines->Add("     M[b].value = first[b];          //the array of vectors called M.");
Memo1->Lines->Add("     M[b+4].value = second[b];       //M of 0 through 16 gets characters");
Memo1->Lines->Add("     M[b+8].value = third[b];");
Memo1->Lines->Add("     M[b+12].value = fourth[b];");
Memo1->Lines->Add("}");
Memo1->Lines->Add("int z = 0;");
Memo1->Lines->Add("int array[129] = {");
Memo1->Lines->Add("-2,-2,-2,-2,-2,1,5,4,   -2,-2,0,4,-2,2,6,5,");
Memo1->Lines->Add("-2,-2,1,5,-2,3,7,6,     -2,-2,2,6,-2,-2,-2,7,");
Memo1->Lines->Add("0,-2,-2,-2,1,5,9,8,     1,0,4,8,2,6,10,9,");
Memo1->Lines->Add("2,1,5,9,3,7,11,10,      3,2,6,10,-2,-2,-2,11,");
Memo1->Lines->Add("4,-2,-2,-2,5,9,13,12,   5,4,8,12,6,10,14,13,");
Memo1->Lines->Add("6,5,9,13,7,11,15,14,    7,6,10,14,-2,-2,-2,15,");
Memo1->Lines->Add("8,-2,-2,-2,9,13,-2,-2,  9,8,12,-2,10,14,-2,-2,");
Memo1->Lines->Add("10,9,13,-2,11,15,-2,-2, 11,10,14,-2,-2,-2,-2,-2 };");
Memo1->Lines->Add(" //These numbers tell the computer 'you can only move horizontally vertically or diagonally and you can only look at these 16 characters.'");
Memo1->Lines->Add("answerarray = new char*[1000];");
Memo1->Lines->Add("");
Memo1->Lines->Add("for (int a = 0; a < 1000; a++)");
Memo1->Lines->Add("        answerarray[a] = new char[16];");
Memo1->Lines->Add("");
Memo1->Lines->Add("");
Memo1->Lines->Add("for (int a = 0; a < 16; a++)");
Memo1->Lines->Add("     for (int b = 0; b < 8; b++){");
Memo1->Lines->Add("        M[a].neighbor[b] = array[z];");
Memo1->Lines->Add("          z++;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//===========================================");
Memo1->Lines->Add("void boggle::confirminput(){");
Memo1->Lines->Add("     cout<<\"make sure you inputted the letters correctly: \"<<endl;");
Memo1->Lines->Add("");
Memo1->Lines->Add("for (int a = 0; a < 16; a++){");
Memo1->Lines->Add("          cout<<M[a].value<<\" \";");
Memo1->Lines->Add("          if (((a+1) % 4) == 0 )");
Memo1->Lines->Add("               cout<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//===========================================");
Memo1->Lines->Add("");
Memo1->Lines->Add("int boggle::findwords(){");
Memo1->Lines->Add("//const int sizeofenglish = 40264;         //  const int sizeofenglish = 40262;");
Memo1->Lines->Add("");
Memo1->Lines->Add("//victorypile = new stack<string>(500);");
Memo1->Lines->Add("char n[40];");
Memo1->Lines->Add("ifstream data;");
Memo1->Lines->Add("int z = 0;");
Memo1->Lines->Add("");
Memo1->Lines->Add("data.open(\"englishrevise.txt\", ios::in);     //split them up");
Memo1->Lines->Add("if (!data)");
Memo1->Lines->Add("data.open(\"wordsfile.txt\", ios::in);     //split them up");
Memo1->Lines->Add("if (!data)");
Memo1->Lines->Add("data.open(\"wordsfile.txt.txt\", ios::in);     //split them up");
Memo1->Lines->Add("if (!data)");
Memo1->Lines->Add("data.open(\"wordsfile\", ios::in);     //split them up");
Memo1->Lines->Add("");
Memo1->Lines->Add("if (!data)");
Memo1->Lines->Add("        return 1;");
Memo1->Lines->Add("");
Memo1->Lines->Add("while (!data.eof()){");
Memo1->Lines->Add("     data>>n;");
Memo1->Lines->Add("     z++;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("sizeofenglish = z;");
Memo1->Lines->Add("english = new listwords[sizeofenglish+2];");
Memo1->Lines->Add("cout<<\"There are: \"<<z<<\" things in this text file.\"<<endl;");
Memo1->Lines->Add("");
Memo1->Lines->Add("data.close();");
Memo1->Lines->Add("data.open(\"englishrevise.txt\", ios::in);     //split them up");
Memo1->Lines->Add("int x = 0;");
Memo1->Lines->Add("while (!data.eof()){");
Memo1->Lines->Add("     data >> english[x].chrword;");
Memo1->Lines->Add("     x++;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("");
Memo1->Lines->Add("for (int s = 0; s < 16; s++){");
Memo1->Lines->Add("clearedges();");
Memo1->Lines->Add("int a = visit(s, -1);   //-1 signifies that we beamed down, we didnt arrive from");
Memo1->Lines->Add("path->pop();                      //anywhere.");
Memo1->Lines->Add("}");
Memo1->Lines->Add("");
Memo1->Lines->Add("data.close();");
Memo1->Lines->Add("return 0;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//==================================");
Memo1->Lines->Add("int boggle::visit(int place, int camefrom){");
Memo1->Lines->Add("     //my problem is I need to undo the restrictions that I place on M[x].edge");
Memo1->Lines->Add("     //when I go back.  I took some of them off, and it works much better. but not");
Memo1->Lines->Add("     //perfect.");
Memo1->Lines->Add("");
Memo1->Lines->Add("     path->push(M[place].value);      //arrive at a spot, put it on the stack.");
Memo1->Lines->Add("");
Memo1->Lines->Add("     int result = binarysearch();    //see if this is a good word or not.");
Memo1->Lines->Add("     if (result == 1){ ");
Memo1->Lines->Add("          //cout<<path->getstringstack()<<\"  \"; ");
Memo1->Lines->Add("          strcpy(answerarray[answercounter], path->getstringstack()); ");
Memo1->Lines->Add("          answercounter++;                                 //was a match");
Memo1->Lines->Add("          int b = 7;");
Memo1->Lines->Add("               for(int c = 0; c < 8; c++){");
Memo1->Lines->Add("                    if (M[place].neighbor[c] != -2)");
Memo1->Lines->Add("                         M[M[place].neighbor[c]].edge[b] = 0;");
Memo1->Lines->Add("                    b--;");
Memo1->Lines->Add("               }");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     else if (result == 0){");
Memo1->Lines->Add("         // cout<<path->getstringstack()<<\":   their may be a match later.\"<<endl;");
Memo1->Lines->Add("          int b = 7;");
Memo1->Lines->Add("               for(int c = 0; c < 8; c++){");
Memo1->Lines->Add("                    if (M[place].neighbor[c] != -2)");
Memo1->Lines->Add("                         M[M[place].neighbor[c]].edge[b] = 0;");
Memo1->Lines->Add("                    b--;");
Memo1->Lines->Add("               }");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     else if (result == -1){");
Memo1->Lines->Add("          //cout<<path->getstringstack()<<\":   no words start with this.\"<<endl;");
Memo1->Lines->Add("          int b = 7;");
Memo1->Lines->Add("               for(int c = 0; c < 8; c++){");
Memo1->Lines->Add("                    if (M[place].neighbor[c] != -2)");
Memo1->Lines->Add("                         M[M[place].neighbor[c]].edge[b] = 1;");
Memo1->Lines->Add("                    b--;");
Memo1->Lines->Add("               }");
Memo1->Lines->Add("");
Memo1->Lines->Add("          //path->pop();");
Memo1->Lines->Add("          return 0;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     for (int i = 0; i < 8; i++){");
Memo1->Lines->Add("          if (M[place].edge[i] == 1){");
Memo1->Lines->Add("               visit(M[place].neighbor[i], i);");
Memo1->Lines->Add("               //undo the restrictions here.");
Memo1->Lines->Add("               path->pop();");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("               int b = 7;");
Memo1->Lines->Add("               for(int c = 0; c < 8; c++){");
Memo1->Lines->Add("                    if (M[place].neighbor[c] != -2)");
Memo1->Lines->Add("                         M[M[place].neighbor[c]].edge[b] = 1;");
Memo1->Lines->Add("                    b--;");
Memo1->Lines->Add("               }");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//==================================");
Memo1->Lines->Add("char *boggle::trunkateword(char *word, int amt){");
Memo1->Lines->Add("char *passback = new char[amt+1];");
Memo1->Lines->Add("strncpy(passback, word, amt);");
Memo1->Lines->Add("passback[amt] = '\0';");
Memo1->Lines->Add("//cout<<\"passback from trunkateword is: \"<<passback<<endl;");
Memo1->Lines->Add("return passback;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//==================================");
Memo1->Lines->Add("int boggle::binarysearch(){");
Memo1->Lines->Add("char *word = new char[path->count() + 1];     //'word' may hold a whole valid word,");
Memo1->Lines->Add("                                              //or just a part of a word, it will be");
Memo1->Lines->Add("                                              //checked against the english via");
Memo1->Lines->Add("                                              //binary search.  path->count makes the");
Memo1->Lines->Add("                                              //size of the array exactly as large as");
Memo1->Lines->Add("                                              //it needs to be.");
Memo1->Lines->Add("//cout<<\"the size of the stack is: \"<<(path->count())<<endl;");
Memo1->Lines->Add("bool keepgoing = false;");
Memo1->Lines->Add("int trunkateletters = path->count();    //when we are looking for a word such as");
Memo1->Lines->Add("                                        // 'supp' we will not find this in the");
Memo1->Lines->Add("                                       //englishwords, we need to trunkate the");
Memo1->Lines->Add("                                        //english words, so we will continue looking");
Memo1->Lines->Add("                                        //for words such as 'supply'.  however");
Memo1->Lines->Add("                                        //we do not want to waste energy looking");
Memo1->Lines->Add("                                        //for words that start with 'zxpim\" because");
Memo1->Lines->Add("                                        //there are none.  trunkate handles this.");
Memo1->Lines->Add("word = path->getstringstack();     //from our stack of characters,");
Memo1->Lines->Add("                                   //getstringstack assembles the chars");
Memo1->Lines->Add("                                   //and puts it into a string.");
Memo1->Lines->Add("//cout<<\"test: \"<<word<<endl;");
Memo1->Lines->Add("int lowindex = 0, highindex = sizeofenglish, endsearch, testnumber;");
Memo1->Lines->Add("                      //lowindex and highindex is how the computer skipps most");
Memo1->Lines->Add("                      //of the 40,000 items, the computer looks half way from");
Memo1->Lines->Add("                      //top to bottom and checks if the word that it wants is up or down,");
Memo1->Lines->Add("                      //if up, then it cuts that in half, and checks the middle.");
Memo1->Lines->Add("//cout<<\"sizeofenglish is: \"<<sizeofenglish<<endl;");
Memo1->Lines->Add("//testnumber = lowindex + ((highindex - lowindex)/2);");
Memo1->Lines->Add("                       //test number is the spot in the 'englishwords' array that");
Memo1->Lines->Add("                       //it is currently checking, this equation cuts the run in");
Memo1->Lines->Add("                       //half, cutting the workload by a million fold.");
Memo1->Lines->Add("");
Memo1->Lines->Add("while (true){");
Memo1->Lines->Add("     testnumber = lowindex + ((highindex - lowindex)/2);");
Memo1->Lines->Add("     endsearch = highindex + lowindex;");
Memo1->Lines->Add("     //cout<<\"our test number is: \"<<testnumber<<\", and our word is: \"<<english[testnumber].chrword<<endl;");
Memo1->Lines->Add("                         //strcmp takes char * only, when the left is smaller than");
Memo1->Lines->Add("                         //the right, the return is less than zero.");
Memo1->Lines->Add("if (strcmp(trunkateword(english[testnumber].chrword, trunkateletters), word) == 0)");
Memo1->Lines->Add("     keepgoing = true;");
Memo1->Lines->Add("if (strcmp(word, english[testnumber].chrword) == 0){");
Memo1->Lines->Add("               //cout<<word<<\" WAS found a perfect match\"<<endl;");
Memo1->Lines->Add("               return 1;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     else if (strcmp(english[testnumber].chrword, word) > 0){");
Memo1->Lines->Add("          highindex = testnumber;//(highindex - (highindex - lowindex) / 2);");
Memo1->Lines->Add("          //cout<<\" We had to shuffle down\"<<endl;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     else if (strcmp(english[testnumber].chrword, word) < 0){");
Memo1->Lines->Add("          lowindex = testnumber; //lowindex + ((highindex - lowindex) / 2);");
Memo1->Lines->Add("          //cout<<\"   We had to shuffle up\"<<endl;");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     else{");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("     if (endsearch == (highindex + lowindex)){");
Memo1->Lines->Add("          if (keepgoing)");
Memo1->Lines->Add("               return 0;");
Memo1->Lines->Add("          //cout<<\"   Endsearch has been reached, no words start with this, nor any matches\"<<endl;");
Memo1->Lines->Add("          return -1;");
Memo1->Lines->Add("               //if highindex or lowindex didnt change then the letters are not here");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("}");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//==================================");
Memo1->Lines->Add("void boggle::displayboard(){");
Memo1->Lines->Add("for (int a = 0; a < 16; a=a+4){");
Memo1->Lines->Add("for(int b = a; b < a+4; b++){");
Memo1->Lines->Add("     cout<<M[b].edge[1]<<\" \";");
Memo1->Lines->Add("     cout<<M[b].edge[0]<<\" \";");
Memo1->Lines->Add("     cout<<M[b].edge[4]<<\"        \";");
Memo1->Lines->Add("}");
Memo1->Lines->Add("cout<<endl;");
Memo1->Lines->Add("for(int b = a; b < a+4; b++){");
Memo1->Lines->Add("     cout<<M[b].edge[2]<<\" \";");
Memo1->Lines->Add("     cout<<M[b].value<<\" \";");
Memo1->Lines->Add("     cout<<M[b].edge[5]<<\"        \";");
Memo1->Lines->Add("}");
Memo1->Lines->Add("cout<<endl;");
Memo1->Lines->Add("for(int b = a; b < a+4; b++){");
Memo1->Lines->Add("     cout<<M[b].edge[3]<<\" \";");
Memo1->Lines->Add("     cout<<M[b].edge[7]<<\" \";");
Memo1->Lines->Add("     cout<<M[b].edge[6]<<\"        \";");
Memo1->Lines->Add("}");
Memo1->Lines->Add("cout<<endl<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("for(int x = 0; x < 16; x=x+4){");
Memo1->Lines->Add("     checkrelations(x, 1, 0, 4);");
Memo1->Lines->Add("     checkrelations(x, 2, 20, 5);");
Memo1->Lines->Add("     checkrelations(x, 3, 7, 6);");
Memo1->Lines->Add("     cout<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//==================================");
Memo1->Lines->Add("void boggle::checkrelations(int startfrom, int position1, int position2, int position3){");
Memo1->Lines->Add("                 //this function displays");
Memo1->Lines->Add("     for (int x = startfrom; x < startfrom+4; x++){");
Memo1->Lines->Add("         if (M[x].neighbor[position1] != -2){");
Memo1->Lines->Add("               if (M[x].neighbor[position1] > 9)");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position1]<<\" \";");
Memo1->Lines->Add("               else");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position1]<<\"  \";");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          else{");
Memo1->Lines->Add("               cout<<\"  \";");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          if (position2 == 20)");
Memo1->Lines->Add("               cout<<M[x].value<<\"  \";");
Memo1->Lines->Add("         else");
Memo1->Lines->Add("          if (M[x].neighbor[position2] != -2){");
Memo1->Lines->Add("               if (M[x].neighbor[position2] > 9)");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position2]<<\" \";");
Memo1->Lines->Add("               else");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position2]<<\"  \";");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          else{");
Memo1->Lines->Add("               cout<<\"  \";");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          if (M[x].neighbor[position3] != -2){");
Memo1->Lines->Add("               if (M[x].neighbor[position3] > 9)");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position3]<<\" \";");
Memo1->Lines->Add("               else");
Memo1->Lines->Add("                    cout<<M[x].neighbor[position3]<<\"  \";");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          else{");
Memo1->Lines->Add("               cout<<\"  \";                       //between letters,");
Memo1->Lines->Add("          }");
Memo1->Lines->Add("          cout<<\"  \";       //between groups of 3,");
Memo1->Lines->Add("     }");
Memo1->Lines->Add("cout<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("void boggle::displaywords(){");
Memo1->Lines->Add("for (int x = 0; x < sizeofenglish; x++){");
Memo1->Lines->Add("     cout<<english[x].chrword<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("cout<<\" sizeofenglish: \"<<sizeofenglish<<endl;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//===============");
Memo1->Lines->Add("void boggle::clearedges(){");
Memo1->Lines->Add("for(int a = 0; a < 16; a++)");
Memo1->Lines->Add("     for(int b = 0; b < 8; b++)");
Memo1->Lines->Add("          M[a].edge[b] = 1;");
Memo1->Lines->Add("for (int a = 0; a < 16; a++){");
Memo1->Lines->Add("if (M[a].x == 0){");
Memo1->Lines->Add("     M[a].edge[0] = 0;");
Memo1->Lines->Add("     M[a].edge[4] = 0;");
Memo1->Lines->Add("     M[a].edge[1] = 0;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("if (M[a].x == 3){");
Memo1->Lines->Add("     M[a].edge[3] = 0;");
Memo1->Lines->Add("     M[a].edge[7] = 0;");
Memo1->Lines->Add("     M[a].edge[6] = 0;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("if (M[a].y == 0){");
Memo1->Lines->Add("     M[a].edge[1] = 0;");
Memo1->Lines->Add("    M[a].edge[2] = 0;");
Memo1->Lines->Add("     M[a].edge[3] = 0;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("if (M[a].y == 3){");
Memo1->Lines->Add("     M[a].edge[4] = 0;");
Memo1->Lines->Add("     M[a].edge[5] = 0;");
Memo1->Lines->Add("     M[a].edge[6] = 0;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//for(int z = 0; z < 16; z++)");
Memo1->Lines->Add("//     M[z].alreadyvisited = false;");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//============================================");
Memo1->Lines->Add("char **boggle::getanswers(char **&answers)");
Memo1->Lines->Add("{");
Memo1->Lines->Add("        for(int x = 0; x < answercounter; x++)");
Memo1->Lines->Add("        {");
Memo1->Lines->Add("               strcpy(answers[x], answerarray[x]);");
Memo1->Lines->Add("        }");
Memo1->Lines->Add("}");
Memo1->Lines->Add("int boggle::getnumanswers()");
Memo1->Lines->Add("{");
Memo1->Lines->Add("        return answercounter;");
Memo1->Lines->Add("}  ");
Memo1->Lines->Add("//===========================");
Memo1->Lines->Add("void boggle::sortanswerwords()");
Memo1->Lines->Add("{");
Memo1->Lines->Add("char **temparray;");
Memo1->Lines->Add("int smallest;");
Memo1->Lines->Add("int sz, location;");
Memo1->Lines->Add("temparray = new char *[1000];");
Memo1->Lines->Add("for(int x = 0; x < 1000; x++)");
Memo1->Lines->Add("{");
Memo1->Lines->Add("        temparray[x] = new char[16];");
Memo1->Lines->Add("}");
Memo1->Lines->Add("for(int x = 0; x < answercounter; x++)");
Memo1->Lines->Add("{");
Memo1->Lines->Add("        smallest = 100;");
Memo1->Lines->Add("        for(int y = 0; y < answercounter; y++)");
Memo1->Lines->Add("        {");
Memo1->Lines->Add("               sz = SizeWord(answerarray[y]);");
Memo1->Lines->Add("                if ((sz != 0) && (sz < smallest))");
Memo1->Lines->Add("                {");
Memo1->Lines->Add("                        smallest = sz;");
Memo1->Lines->Add("                        location = y;");
Memo1->Lines->Add("               }");
Memo1->Lines->Add("        }");
Memo1->Lines->Add("        strcpy(temparray[x], answerarray[location]);");
Memo1->Lines->Add("        strcpy(answerarray[location], \"\");");
Memo1->Lines->Add("}");
Memo1->Lines->Add("for(int x = 0; x < answercounter; x++)");
Memo1->Lines->Add("{");
Memo1->Lines->Add("        strcpy(answerarray[x], temparray[x]);");
Memo1->Lines->Add("}");
Memo1->Lines->Add("}");
Memo1->Lines->Add("int boggle::SizeWord(char *word)");
Memo1->Lines->Add("{ ");
Memo1->Lines->Add("string n; ");
Memo1->Lines->Add("n = word;  ");
Memo1->Lines->Add("return n.length(); ");
Memo1->Lines->Add("}");
Memo1->Lines->Add("//This is the Code for a Console program boggle!  If you email me or use aim: maeon3 maybe you can entice me to give you copy of the cpp and header files of this GUI boggle and Unjumbler solver. ");

}
//===========================
void __fastcall TForm1::ShowAllWords1Click(TObject *Sender)
{
boggle myboggle;

HANDLE  hThrd;
DWORD      Id;

hEvent = CreateEvent ( NULL , false , false , "my event" );

hThrd=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThrdFunc,(LPVOID)0,0,&Id);
if ( !hThrd )
{
//cout<<"Error Creating Thread ..."<<endl;
return;
}
Memo1->Visible = false;
Label4->Visible = true;
Label4->Caption = "You have selected to Display All the Words in wordsfile.txt this is 1.5 Mb worth of Text that holds about 170000 english words (some greek), The viewbox has been made invisible to greatly speed up the process it will reappear when done.";
Label4->Refresh();

SetEvent ( hEvent );


char **answers = new char*[180000];
for(int a = 0; a < 180000; a++)
        answers[a] = new char[16];
int sz = myboggle.ReturnAllWords(answers);

char *longstring = new char[1000000];
Memo1->Text = "";

Memo1->Visible = false;
for(int x = 0; x < sz; x++)
{
        //strcat(longstring, answers[x]);
         Memo1->Lines->Add(answers[x]);
        // Sleep ( 2000 );
}
Memo1->Visible = true;
Label4->Visible = false;
Form1->WindowState = wsMaximized;
Memo1->Lines->Add("--Done--");


}
//---------------------------------------------------------------------------

